import axios from 'axios';
import httpCode from './httpCode.js'
import common from './common.js'
const instance = axios.create({
	baseURL: common.BASE_URL
});
//http://180.76.168.66/tinyWeather
//http://127.0.0.1:8848
// Alter defaults after instance has been created

instance.interceptors.request.use(function(config) {
const key=localStorage.getItem('key')
if(key!=undefined&&key!=''){
	config.headers.authKey=key;
// instance.defaults.headers.common['authKey'] = key;
}
	//console.log('--------------req------------', config)
	// Do something before request is sent
	return config;
}, function(error) {
	// Do something with request error
	return Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function(response) {
	//console.log('--------------rep------------', response)
	// Any status code that lie within the range of 2xx cause this function to trigger
	// Do something with response data
	httpCode(response.data.code)
	return response;
}, function(error) {
	httpCode(error.response.status)
	// Any status codes that falls outside the range of 2xx cause this function to trigger
	// Do something with response error
	return Promise.reject(error);
});

export default instance
